package com.yyj.mybties.jdbc;


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取数据库中表的结构
 */
public class JDBCdemo {
    public static void main(String[] args) {
    String driver = "com.mysql.jdbc.Driver";
    String URL = "jdbc:mysql://localhost:3306/demo_ds_master";
    Connection con = null;
    List<String> tableNames = new ArrayList();
    try {
        con= DriverManager.getConnection(URL,"root","root");
        sql(con);
//        getTableInfo(con,"users");
    } catch(Exception e) {
        System.out.println("Connect fail:" + e.getMessage());
    }
}
    private static void getTableInfo(Connection con,String tableName) throws SQLException {
        DatabaseMetaData metaData = con.getMetaData();
        ResultSet colRet = metaData.getColumns(null, "%", tableName, "%");
        String columnName;
        String columnType;
        while(colRet.next()){
            columnName = colRet.getString("COLUMN_NAME");
            columnType = colRet.getString("TYPE_NAME");
            int datasize = colRet.getInt("COLUMN_SIZE");
            int digits = colRet.getInt("DECIMAL_DIGITS");
            int nullable = colRet.getInt("NULLABLE");
            System.out.println(columnName+" "+columnType+" "+datasize+" "+digits+" "+ nullable);
        }
        System.out.println("\n");
    }
    private static void sql(Connection con) throws SQLException {
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from t_order");
        while(rs.next()){
           System.out.println(rs.getInt(1)+":"+rs.getInt(2));
          }

        //关闭资源
         rs.close();
        st.close();
        con.close();
        System.out.println();
    }
}
